﻿using EkStudio.com.bean.xml.reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EkStudio.com.bean.xml.RootInterface.Plugin
{
    public class BillExpandJsCodeRoot:BillUiFormatContainer
    {
        private int maxid = 0;
        private List<BillExpandJsCodeItem> item;

        [Xml("maxid")]
        public int Maxid
        {
            get
            {
                return maxid;
            }

            set
            {
                maxid = value;
            }
        }
        [XmlElement("item")]
        public List<BillExpandJsCodeItem> Item
        {
            get
            {
                return item;
            }

            set
            {
                item = value;
            }
        }
        [XmlTransient]
        public object ParentBean
        {
            get
            {
                return null;
            }

            set
            {
                
            }
        }

        public object Clone()
        {
            BillExpandJsCodeRoot clone = new BillExpandJsCodeRoot();
            List<BillExpandJsCodeItem> list = new List<BillExpandJsCodeItem>();
            if (this.item != null && this.item.Count > 0)
            {
                for (int i = 0; i < item.Count; i++)
                {
                    Object it = item[i].Clone();
                    if (it is BillExpandJsCodeItem){
                    list.Add((BillExpandJsCodeItem)it);
                }
            }
        }
        clone.Item = list ;
		return clone;
        }


        #region 容器接口实现

        public void addNode(BillUiFormatNode node, BillFormatModule format)
        {
            if (item == null) item = new List<BillExpandJsCodeItem>();
            BillExpandJsCodeItem code = node as BillExpandJsCodeItem;
            code.LanguageTexts = this.getNLanguageText(format);
            this.item.Add(code);
        }

        public void removeNode(BillUiFormatNode node)
        {
            if (this.item != null && this.item.Contains(node as BillExpandJsCodeItem))
            {
                this.item.Remove(node as BillExpandJsCodeItem);
            }
        }

        public void moveUp(BillUiFormatNode node)
        {
            throw new NotImplementedException();
        }

        public void moveDown(BillUiFormatNode node)
        {
            throw new NotImplementedException();
        }

        public string getNLanguageText(BillFormatModule format)
        {
            String result = "";
            String language = format.getLanguageText();
            if (language.Length > 0)
            {
                int index = language.IndexOf("Caption");
                if (index != -1)
                {
                    int length = language.Length - 7;
                    String first = language.Substring(0, length);
                    int max = maxid + 1;
                    this.Maxid = max;
                    result = first + "Js[" + max + "].Caption";
                }
            }
            return result;
        }

        public void updateLanguageText(BillFormatModule format)
        {
            this.maxid = 0;
            if (this.item != null && this.item.Count > 0)
            {
                foreach(BillExpandJsCodeItem js in this.item)
                {
                    js.LanguageTexts = this.getNLanguageText(format);
                }
            }
        }

        #endregion
    }
}
